/**
 *
 */
package task;


import com.sun.org.slf4j.internal.Logger;
import com.sun.org.slf4j.internal.LoggerFactory;
import context.TaskContext;
import event.LogEvent;
import tasklog.LogEventPublisher;
import tasklog.LogQueue;
import tasklog.TaskLogDetailEvent;
import tasklog.TaskLogEvent;

/**
 * 抽象任务处理器
 *
 * @author scotthu
 * @date 2018年2月26日
 */
public abstract class AbstractTaskHandler implements TaskHandler {

    private static final Logger logger = LoggerFactory.getLogger(AbstractTaskHandler.class);

    protected LogEventPublisher publisher;

    protected TaskContext taskContext;

    public AbstractTaskHandler() {

    }

    public AbstractTaskHandler bindContext(TaskContext ctx) {
        this.taskContext = ctx;
        return this;
    }

    public AbstractTaskHandler bindPublisher(LogEventPublisher publisher) {
        this.publisher = publisher;
        return this;
    }

    /**
     * 发送任务日志
     *
     * @param content
     */
    public void publishTaskEvent(TaskLogEvent taskLog) {
        if (publisher == null) {
            logger.warn("task event publisher does not exist");
            logger.debug("task info:{}", taskLog);
            return;
        }

        LogEvent<TaskLogEvent> event = new LogEvent<>(LogQueue.TASK_LOG_QUEUE, taskLog);
        publisher.publish(event);
        logger.debug("send a task event:{}", event);
    }

    public void publishTaskDetailEvent(TaskLogDetailEvent detail) {
        if (publisher == null) {
            logger.warn("task event publisher does not exist");
            logger.debug("task info:{}", detail);
            return;
        }
        LogEvent<TaskLogDetailEvent> eventDetail = new LogEvent<>(LogQueue.TASK_LOG_DETAIL_QUEUE, detail);
        publisher.publish(eventDetail);
    }

}
